﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;

namespace PostTestAnswers
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class PostTestAnswers : IPostTestAnswers
    {
        public bool postTestAnswers(int testId, int userId, string answers)
        {
            bool successful = false;
            SqlConnection myConnection = new SqlConnection(getSqlConnectionString());
            myConnection.Open();

            try
            {
                SqlCommand myCommand = new SqlCommand("INSERT INTO AssessmentGrades (testId, userId, answers) " +
                        "VALUES ('" + testId + "', '" + userId + "', '" + answers + "')", myConnection);
                myCommand.ExecuteNonQuery();
                successful = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            myConnection.Close();
            return successful;
        }

        private string getSqlConnectionString()
        {
            return @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestingServices.mdf;Integrated Security=True;User Instance=True";
        }
    }
}
